package main

import (
	"CaptchaCluster/client/conf"
	"CaptchaCluster/client/game"
	"CaptchaCluster/client/gate"
	"CaptchaCluster/client/machine"
	"CaptchaCluster/client/redisManager"
	"CaptchaCluster/cmd"
	"CaptchaCluster/log"
	"os"
	"os/signal"
	"syscall"
	"time"

	_ "net/http/pprof"
)

func main() {
	// 性能检测
	//runtime.SetMutexProfileFraction(1)
	//runtime.SetBlockProfileRate(1)
	//go func() {
	//	http.ListenAndServe("localhost:1010", nil)
	//}()

	// 初始化配置
	conf.Init()

	// 启动redis
	redisManager.Start()
	startCapthca()

	// 开启机器状态读取
	machine.Init()

	// 启动远程服务
	game.Module.OnInit()
	closeSig := make(chan bool, 1)
	go game.Module.Run(closeSig)
	gate.Init(game.ChanRPC)

	signCh := make(chan os.Signal, 1)
	signal.Notify(signCh, syscall.SIGKILL, syscall.SIGINT, syscall.SIGQUIT)
	<-signCh

	log.Release("正在关闭redis,关闭过程会窗口假死,千万不要使用任务管理器结束进程.")
	redisManager.Stop()
	log.Close()
}

// 启动python业务
func startCapthca() {
	log.Release("start the captcha python script")
	seekOut, err := cmd.StartWithKeyword(time.Second*10, cmd.GBK, "WARNING", "cd yidun_slide", "app.py")
	if err != nil {
		log.Fatal("fatal error starting python script:%v", err)
	} else if !seekOut {
		log.Fatal("fatal captcha configuration error,possibly port")
	}
	log.Release("starting captcha python script complete")
}
